package com.ronds.webtest.util;

import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by zhanghui on 2017/8/23.
 * 时间工具
 */
public class DateUtil {

    private static final Logger logger = Logger.getLogger(DateUtil.class);

    public static long getMillis() {
        return System.currentTimeMillis()%100000;
    }

    public static String getYearMonthDay(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static String getHour() {
        Calendar now =Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(now);
    }

    public static String getYearMonthDay() {
        return getYearMonthDay(new Date());
    }

    public static String getMonthDay(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd");
        return sdf.format(date);
    }

    public static String getMonthDay() {
        return getMonthDay(new Date());
    }

    public static String fromYMDtoYMDWithSlice(String strYMD) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd");
        String strYMDWithSlice = null;
        try {
            Date date = sdf1.parse(strYMD);
            strYMDWithSlice = sdf2.format(date);
        } catch (ParseException e) {
            logger.error(e);
        }
        return strYMDWithSlice;
    }

    public static String fromYearMonthDay2MonthDay(String strYearMonthDay) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("MM-dd");
        String strMonthDay = null;
        try {
            Date date = sdf1.parse(strYearMonthDay);
            strMonthDay = sdf2.format(date);
        } catch (ParseException e) {
            logger.error(e);
        }
        return strMonthDay;
    }
}
